﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MyFitness.Model;

namespace MyFitness.View
{
    public partial class MealEntryForm : Form
    {
        #region Declared Variables
        private MyFitnessDatabaseEntities database;
        private Meal meal;
        #endregion

        /// <summary>
        /// Default Constructor.
        /// </summary>
        public MealEntryForm(MyFitnessDatabaseEntities db)
        {
            meal = new Meal();

            Initialize(db);
        }

        /// <summary>
        /// Constructor to load the form with data.
        /// </summary>
        /// <param name="m"></param>
        public MealEntryForm(MyFitnessDatabaseEntities db, Meal m)
        {
            meal = m;

            Initialize(db);
        }

        /// <summary>
        /// Initializes information for either constructor.
        /// </summary>
        private void Initialize(MyFitnessDatabaseEntities db)
        {
            InitializeComponent();
            CenterToParent();

            // Closes the form if a null database is inputted.
            if (db == null)
                Close();

            // Connects to the database to pull exercise types
            database = db;

            // Create handlers for tracking when an Exercise Entry is validated for saving an entry.
            MealTypeComboBox.SelectedValueChanged += new EventHandler(OnEnableOKButton);
            MealComboBox.SelectedValueChanged += new EventHandler(OnEnableOKButton);
            ServingsConsumedTextBox.TextChanged += new EventHandler(OnEnableOKButton);
        }

        /// <summary>
        /// Gets the Meal Object from the information entered into the dialog.
        /// </summary>
        /// <returns>Returns a Meal Object</returns>
        public Meal GetMealEntry()
        {
            try
            {
                meal.MealTypeID = Convert.ToInt32(MealTypeComboBox.SelectedValue);
                meal.Servings = Convert.ToInt32(ServingsConsumedTextBox.Text);

                FoodAndBeverage fb = database.FoodAndBeverages.Find((int)MealComboBox.SelectedValue);
                meal.FoodAndBeverages.Add(fb);

                return meal;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return null;
        }

        /// <summary>
        /// Load Form Event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MealEntryForm_Load(object sender, EventArgs e)
        {
            try
            {

                // Loads the possible MealTypes into the MealTypeComboBox.
                MealTypeComboBox.DataSource = database.MealTypes.ToList();
                MealTypeComboBox.DisplayMember = "Name";
                MealTypeComboBox.ValueMember = "Id";

                // Loads the possible Meals into the MealComboBox.
                MealComboBox.DataSource = database.FoodAndBeverages.ToList();
                MealComboBox.DisplayMember = "Name";
                MealComboBox.ValueMember = "Id";

                // Error checking to prevent null values
                if( meal.MealTypeID > 0 )
                    MealTypeComboBox.SelectedValue = meal.MealTypeID;
                
                ServingsConsumedTextBox.Text = meal.Servings.ToString();

                // Error checking to prevent null values
                if( meal.FoodAndBeverages.FirstOrDefault() != null )
                    MealComboBox.SelectedValue = meal.FoodAndBeverages.FirstOrDefault().Id;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        /// <summary>
        /// Enables or Disables the OKButton depending on validated input.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnEnableOKButton(object sender, EventArgs e)
        {
            // Enables the "OK" Button if all the fields are validated.
            if (MealTypeComboBox.SelectedItem != null && MealComboBox.SelectedItem != null && ServingsConsumedTextBox.Text.Length > 0)
                okButton.Enabled = true;
            else
                okButton.Enabled = false;
        }

        /// <summary>
        /// Loads the Nutrition Facts into all the controls that support it in the form.
        /// </summary>
        /// <param name="fb"></param>
        private void LoadNutritionFacts(FoodAndBeverage fb)
        {
            ServingSizeTextBox.Text = fb.Servings.ToString();
            CaloriesTextBox.Text = fb.Calories.ToString();
            CaloriesFromFatTextBox.Text = fb.CaloriesFromFat.ToString();
            TotalFatTextBox.Text = fb.TotalFat.ToString();
            SaturatedFatTextBox.Text = fb.SaturatedFat.ToString();
            TransFatTextBox.Text = fb.SaturatedFat.ToString();
            CholesterolTextBox.Text = fb.Cholesterol.ToString();
            SodiumTextBox.Text = fb.Sodium.ToString();
            TotalCarbohyrdateTextBox.Text = fb.Carbohydrates.ToString();
            DietaryFiberTextbox.Text = fb.DietaryFiber.ToString();
            SugarsTextBox.Text = fb.DietaryFiber.ToString();
            ProteinTextBox.Text = fb.Protein.ToString();
            VitaminATextBox.Text = fb.VitaminA.ToString();
            VitaminCTextBox.Text = fb.VitaminC.ToString();
            CalciumTextBox.Text = fb.Calcium.ToString();
            IronTextBox.Text = fb.Iron.ToString();
        }

        /// <summary>
        /// Traps for when MealComboBox changes the selected item.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MealComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int fb_id = Convert.ToInt32(MealComboBox.SelectedValue);
                FoodAndBeverage fb = database.FoodAndBeverages.Find(fb_id);
                LoadNutritionFacts(fb);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
